local chenguang = fk.CreateSkill{
  name = "xiaobai__chenguang",
  dynamic_desc = function (self, player, lang)
    if player:getMark("@@xiaobai__chenguang") > 0 then
      return "xiaobai__chenguang_fix"
    else
      return "xiaobai__chenguang"
    end
  end,
}

Fk:loadTranslationTable{
  ["xiaobai__chenguang"] = "沉光",
  [":xiaobai__chenguang"] = "你受到伤害时，若你本轮未获得过牌，你可以摸一张牌，防止之；"..
    "你回复体力时，若你本轮未失去过牌，你可以弃置一张牌，翻倍之。牌堆洗牌后，<a href=':xiaobai__chenguang_fix'>修改</a>此技能。",
  ["@@xiaobai__chenguang"] = "沉光 已修改",
  ["#xiaobai__chenguang_recover1_prompt"] = "沉光：你可以弃置一张牌，额外回复 %arg 点体力",
  ["#xiaobai__chenguang_recover2_prompt"] = "沉光：你可以摸一张牌，额外回复 %arg 点体力",
  ["#xiaobai__chenguang_damage1_prompt"] = "沉光：你可以摸一张牌，防止此伤害",
  ["#xiaobai__chenguang_damage2_prompt"] = "沉光：你可以弃一张牌，防止此伤害",
  ["xiaobai__chenguang_fix"] = "沉光",
  [":xiaobai__chenguang_fix"] = "你受到伤害时，若你本轮获得过牌，你可以弃置一张牌，防止之；你回复体力时，若你本轮失去过牌，你可以摸一张牌，翻倍之。"
}

chenguang:addEffect(fk.PreHpRecover, {
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    if target == player and player:hasSkill(self) then
      return #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == player and move.to ~= player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                return true
              end
            end
          end
        end
      end, Player.HistoryRound) == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@xiaobai__chenguang") > 0 then
    return room:askToSkillInvoke(player,{skill_name = chenguang.name, prompt = "#xiaobai__chenguang_recover2_prompt"})
    else
        local cards = room:askToDiscard(player,{ 
          max_num = 1,
          min_num = 1,
          include_equip = true,
          skill_name = chenguang.name,
          cancelable = true, 
          prompt = "#xiaobai__chenguang_recover1_prompt:::"..data.num,
          skip = true})
        if #cards > 0 then
          event:setCostData(self, cards[1])
          return true
        end
      end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@xiaobai__chenguang") > 0 then
      player:drawCards(1, chenguang.name)
    else
      room:throwCard(event:getCostData(self), chenguang.name, player)
    end
    data.num = data.num * 2
  end
})

chenguang:addEffect(fk.DamageInflicted, {
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    if target == player and player:hasSkill(self) then
      return #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.to == player and move.from ~= player 
          and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip) then
            return true
          end
        end
      end, Player.HistoryRound) == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@xiaobai__chenguang") > 0 then
      local cards = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = chenguang.name,
        prompt = "#xiaobai__chenguang_damage2_prompt:::"..data.damage, true})
      if #cards > 0 then
        event:setCostData(self, cards[1])
        return true
      end
    else
      return room:askToSkillInvoke(player,{
        skill_name = chenguang.name,
        prompt = "#xiaobai__chenguang_damage1_prompt"})
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("@@xiaobai__chenguang") > 0 then
      room:throwCard(event:getCostData(self), chenguang.name, player)
    else
      player:drawCards(1, chenguang.name)
    end
    data:preventDamage()
  end
})

chenguang:addEffect(fk.AfterDrawPileShuffle, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:getMark("@@xiaobai__chenguang") == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@@xiaobai__chenguang", 1)
  end
})

return chenguang